﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EO.Internal;
using Models;

namespace DAL
{
    public class WeiBoService
    {
        public static List<dynamic> SelectMyWbContent(int Uid)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                var sql = from u in db.Users
                          from m in db.MicroBlog
                          where m.U_ID == u.U_ID  && u.U_ID == Uid
                          select new
                          {                             
                              m.M_Content,
                              m.M_FrowardingTimes,
                              m.M_DataTime,
                              u.U_Nick,
                              u.UserInfo_Image,
                              m.M_ID
                          };
                List<dynamic> list = new List<dynamic>();
                foreach (var item in sql.ToList())
                {
                    dynamic d = new ExpandoObject();
                    d.M_Content = item.M_Content;
                    d.M_FrowardingTimes = item.M_FrowardingTimes;
                    d.M_DataTime = item.M_DataTime;
                    d.U_Nick = item.U_Nick;
                    d.UserInfo_Image = item.UserInfo_Image;
                    d.M_ID = item.M_ID;
                    list.Add(d);
                }
                return list;
            }
        }
        public static List<dynamic> SelectUserLabel(int Uid)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                var sql = from u in db.Users
                          from l in db.UserLable
                          where l.U_ID == u.U_ID && u.U_ID == Uid
                          select new
                          {
                             l.UL_Content
                          };
                List<dynamic> list = new List<dynamic>();
                foreach (var item in sql.ToList())
                {
                    dynamic d = new ExpandoObject();                      
                    d.UL_Content = item.UL_Content;
                    list.Add(d);
                }
                return list;
            }
        }

        public static bool InsertLabel(UserLable userLable)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                db.UserLable.Add(userLable);
                return db.SaveChanges() > 0;
            }
        }
        public static List<dynamic> SelectUserInfo(int? Uid)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                var sql = from u in db.Users
                          from i in db.UserInfo
                          where i.U_ID == u.U_ID && u.U_ID == Uid
                          select new
                          {
                              u.U_ID,
                              u.U_Nick,
                              u.U_Email,
                              u.UserInfo_Image,
                              i.UserInfo_realname,
                              i.UserInfo_Address,
                              i.UserInfo_SchoolType,
                              i.UserInfo_SchoolName,
                              i.UserInfo_CompanyName,
                              i.UserInfo_DepartmentPosition,
                          };
                List<dynamic> list = new List<dynamic>();
                foreach (var item in sql.ToList())
                {
                    dynamic d = new ExpandoObject();
                    d.U_ID = item.U_ID;
                    d.U_Nick = item.U_Nick;
                    d.U_Email = item.U_Email;
                    d.UserInfo_realname = item.UserInfo_realname;
                    d.UserInfo_Address = item.UserInfo_Address;
                    d.UserInfo_Image = item.UserInfo_Image;
                    d.UserInfo_SchoolType = item.UserInfo_SchoolType;
                    d.UserInfo_SchoolName = item.UserInfo_SchoolName;
                    d.UserInfo_CompanyName = item.UserInfo_CompanyName;
                    d.UserInfo_DepartmentPosition = item.UserInfo_DepartmentPosition;
                    list.Add(d);
                }
                return list;
            }
        }
        
        
        public static List<dynamic> SelectMyWbComment(int WbID)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                var sql = from u in db.Users
                          from m in db.MicroBlog
                          from c in db.Comment
                          from i in db.UserInfo
                          where c.U_ID == u.U_ID && c.M_ID == m.M_ID && i.U_ID == u.U_ID && m.M_ID == WbID
                          select new
                          {
                              c.C_Content,
                              u.U_Nick,
                              c.C_Time,
                              i.UserInfo_Image
                          };
                List<dynamic> list = new List<dynamic>();
                foreach (var item in sql.ToList())
                {
                    dynamic d = new ExpandoObject();
                    d.C_Content = item.C_Content;
                    d.U_Nick = item.U_Nick;
                    d.C_Time = item.C_Time;
                    d.UserInfo_Image = item.UserInfo_Image;
                    list.Add(d);
                }
                return list;
            }
        }

        public static List<dynamic> SelectUserFollow(int? Uid)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                var sql = (from f in db.UserFollow
                           from u in db.Users
                           where f.U_ID == u.U_ID && u.U_ID == Uid
                           select new
                           {
                               f.UFollow_ID,
                               U_Nick = (
                                  from u in db.Users
                                  where u.U_ID == f.UFollow_ID
                                  select new
                                  {
                                      u.U_Nick
                                  }).ToList(),
                               UserInfo_Image = (
                                  from u in db.Users
                                  where u.U_ID == f.UFollow_ID
                                  select new
                                  {
                                      u.UserInfo_Image
                                  }).ToList(),
                              U_ID = (
                                   from u in db.Users
                                   where u.U_ID == f.UFollow_ID
                                   select new
                                   {
                                       u.U_ID
                                   }).ToList()
                           }).ToList();
                List<dynamic> list = new List<dynamic>();
                foreach (var item in sql.ToList())
                {
                    dynamic d = new ExpandoObject();
                    d.U_Nick = item.U_Nick[0].U_Nick;
                    d.UserInfo_Image = item.UserInfo_Image[0].UserInfo_Image;
                    d.U_ID = item.U_ID[0].U_ID;
                    list.Add(d);
                }
                return list;
            }
        }
        public static List<dynamic> SelectUserFans(int Uid)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                var sql = from u in db.Users
                          from s in db.UserFans
                          where s.U_ID == u.U_ID && u.U_ID == Uid
                          select new
                          {
                              s.FansID,
                              s.UFans_ID,
                              u.U_ID,
                              u.U_Nick,
                              u.UserInfo_Image
                          };
                List<dynamic> list = new List<dynamic>();
                foreach (var item in sql.ToList())
                {
                    dynamic d = new ExpandoObject();
                    d.FansID = item.FansID;
                    d.UFans_ID = item.UFans_ID;
                    d.U_ID = item.U_ID;
                    d.U_Nick = item.U_Nick;
                    d.UserInfo_Image = item.UserInfo_Image;
                    list.Add(d);
                }
                return list;
            }
        }

        public static bool DeleteFollow(int? Uid)
        {
            using (MicroBlogEntities db=new MicroBlogEntities())
            {
                var result = db.UserFollow.SingleOrDefault(f => f.UFollow_ID == Uid);
                if (result!=null)
                {
                    db.UserFollow.Remove(result);
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }
        
    }
}
